package com.sinothk.cloud.mq.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/websocketIm/{userId}/{device}")
@Component
public class WebSocketIMConfig {

    private static final Logger LOG = LoggerFactory.getLogger(WebSocketIMConfig.class);

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") String userId,@PathParam(value = "device") String device) {
//        webClients.put(clientId, session);

//        UserBean userBean = MemCons.userBeans.get(userId);
//        if (userBean == null) {
//            userBean = new UserBean(userId, avatar);
//        }
////        if (device == 0) {
////            userBean.setPhoneSession(session, 0);
////            userBean.setPhone(true);
////            LOG.info("Phone用户登陆:" + userBean.getUserId() + ",session:" + session.getId());
////        } else {
//        userBean.setPcSession(session, 1);
//        userBean.setPhone(false);
////            LOG.info("PC用户登陆:" + userBean.getUserId() + ",session:" + session.getId());
////        }
////        this.userId = userId;
//
//        //加入列表
//        MemCons.userBeans.put(userId, userBean);
    }

    @OnClose
    public void onClose(Session session, @PathParam(value = "clientId") String clientId) {
//        webClients.remove(clientId);
        LOG.error("onClose");
    }

    @OnError
    public void OnError(Session session, Throwable throwable, @PathParam(value = "clientId") String clientId) {
//        webClients.remove(clientId);
        LOG.error(throwable.getMessage());
    }

    @OnMessage
    public void onMessage(String message, Session session, @PathParam(value = "clientId") String clientId) throws Exception {
//			if(message.contains("#")) {
//				String[] msgArr = message.split("#");
//				sendMsg(msgArr[0], clientId+"向你发送了数据:"+msgArr[1]);
//			} else if(message.contains("@")){
//
//			} else if(message.contains("!")){
//
//			} else {
//				sendMsg(clientId, message);
//			}
        handleMessage(session, message);
    }

    private void handleMessage(Session session, String message) {
//        EventData data;
//        try {
//            data = gson.fromJson(message, EventData.class);
//        } catch (JsonSyntaxException e) {
//            System.out.println("json解析错误：" + message);
//            return;
//        }
//
//        switch (data.getEventName()) {
//            case "__invite":
//                invite(message, data.getData());
//                break;
//            case "__answer":
//                answer(message, data.getData());
//
//            case "__ice_candidate":
//                iceCandidate(message, data.getData());
//                break;
//        }
    }
}
